package com.hy.erp.inventory.service;

import java.util.List ;
import java.util.Map ;

import javax.ejb.Stateless ;

import org.jboss.seam.ScopeType ;
import org.jboss.seam.annotations.Name ;
import org.jboss.seam.annotations.Scope ;

import com.hy.enterprise.architecture.foundation.ArchitectureFoundationException;
import com.hy.enterprise.framework.service.business.AbstractBusinessService;
import com.hy.erp.inventory.dao.interfaces.IInventoryDao;
import com.hy.erp.inventory.pojo.Inventory;
import com.hy.erp.inventory.pojo.interfaces.IInventory;
import com.hy.erp.inventory.service.interfaces.IInventoryService;
import com.hy.erp.inventory.vsoft.libra.enterprise.framework.util.LibraUtil;

@ Name ( "inventoryService" )
@ Scope ( ScopeType.STATELESS )
@ Stateless
public class InventoryService extends AbstractBusinessService < IInventory > implements IInventoryService{

	@ Override
	public boolean addInventory (
									IInventory inventory )
	{

		if ( null == inventory)
		{
			return true ;
		}
		
		else
		{
			System.out
						.println ("ssssssssssssssssssssssffffffffffffffffhghhhhhhhhhhhhhhhhhhhhhh") ;


			IInventoryDao inventoryDao =
			                       ( IInventoryDao ) this
			                                          .getDao ( IInventory.class ) ;
			
			return inventoryDao
			                .persist ( inventory ) ;
		}
		
	
	}

	@ Override
	public boolean modifyInventory (
										IInventory inventory )
	{

		if ( ( null == inventory )
			     || ( null == inventory
			                                .getIdentifier ( ) )
			     || ( inventory
			                        .getIdentifier ( )
			                        .trim ( )
			                        .length ( ) == 0 ) )
			{
				return true ;
			}
			else
			{
				IInventoryDao inventoryDao =
                    ( IInventoryDao ) this
                                       .getDao ( IInventory.class ) ;
		
					
				if ( null == inventory )
				{
					throw new ArchitectureFoundationException  (
					                                            "修改实体数据时发生错误，所需要修改的实体在数据库中并不存在" ) ;
				}
				
				
				return ( inventoryDao
				                  .merge ( inventory ) != null ) ;
			}
		
	
	}

	@ Override
	public Integer removeInventory (
										String [ ] inventoryIds )
	{

		if ( ( null == inventoryIds )
			     || ( inventoryIds.length == 0 ) )
			{
				return new Integer (
				                     0 ) ;
			}
			else
			{IInventoryDao inventoryDao =
                ( IInventoryDao ) this
                .getDao ( IInventory.class ) ;
				return inventoryDao
				                .remove ( inventoryIds ) ;
			}
	
	}

	@ Override
	public List < Inventory > findByNamedQuery (String queryName,
										
											Map < String , Object > parameters )
	{
		if ( null ==queryName &&null == parameters )
		{
			return null ;
		}
		else
		{
		
			IInventoryDao inventoryDao =
                ( IInventoryDao ) this
                .getDao ( IInventory.class ) ;
			return inventoryDao
			                .findByNamedQueryAndNamedParams (
			                                                  queryName,parameters
			                                                   ) ;
		

	}

}

	@ Override
	public Inventory findUniqueResultByNamedQuery (
													String queryName ,
													Map < String , Object > parameters )
	{
		if ( null ==queryName &&null == parameters )
		{
			return null ;
		}
		else
		{
		Object o=	parameters.get ( "identifier" );
			IInventoryDao inventoryDao =
                ( IInventoryDao ) this
                .getDao ( IInventory.class ) ;
			return inventoryDao
			                .findUniqueResultByNamedParams (
			                                                  queryName,
			                                                  LibraUtil
			                                                           .makeNamedParameter (
			                                                                                 "identifier" ,
			                                                                                 o ) ) ;
	}
		}

	@ Override
	public String [ ] addInventory (
										Object [ ] inventory )
	{


		if ( null == inventory)
		{
			return null ;
		}
		
		else
		{
			
			IInventoryDao inventoryDao =
			                       ( IInventoryDao ) this
			                                          .getDao ( IInventory.class ) ;

			Inventory[] inventorys=new Inventory[inventory.length];
						for(int i=0;i<inventory.length;i++)
						{
							Inventory inventory2=( Inventory ) inventory[i];
							inventorys[i]=inventory2;
						}
						return inventoryDao
						                .persist ( inventorys ) ;
			
		}
		
	
	
	}

	@ Override
	public List < IInventory > getAllInventory ( )
	{
		IInventoryDao inventoryDao =
            ( IInventoryDao ) this
            .getDao ( IInventory.class ) ;
			return inventoryDao
			                .find ( );
	}

	@ Override
	public String [ ] modifyInventory (
										Object [ ] inventory )
	{



		if ( null == inventory)
		{
			return null ;
		}
		
		else
		{
			
			IInventoryDao inventoryDao =
			                       ( IInventoryDao ) this
			                                          .getDao ( IInventory.class ) ;

			Inventory[] inventorys=new Inventory[inventory.length];
						for(int i=0;i<inventory.length;i++)
						{
							Inventory inventory2=( Inventory ) inventory[i];
							inventorys[i]=inventory2;
						}
						return inventoryDao
						                .merge  ( inventorys ) ;
			
		}
		
	
		

	
	}
	@Override
	public IInventory findMaterielCount(String queryName, String[]Ids) {

		IInventoryDao inventoryDao =
            						( IInventoryDao ) this
            											.getDao ( IInventory.class ) ;
		return inventoryDao.findUniqueResultByNamedQuery ( queryName , Ids ); 
	}
	}
